package com.example.mobile2021606134chhnewdemo.NewsModule;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.mobile2021606134chhnewdemo.R;

import java.util.List;

/**
 * @ClassName: NewsRViewAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 27/6/2021$
 * @Version: 1.0
 */
public class NewsRViewAdapter extends RecyclerView.Adapter<NewsRViewAdapter.ViewHolder> {
    List<News> list;
    private Context context;
    public NewsRViewAdapter(List<News> list, Context context) {
//        Log.i("TAG", "new NewsRViewAdapter");
        this.list = list;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        Log.i("TAG", String.valueOf(viewType));
//        if (list.get(viewType).getCategory().equals(News.IMAGE_NEWS)) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_news_pic, null);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
//        } else {
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_news_dpic, null);
//            ViewHolder viewHolder = new ViewHolder(view);
//            return viewHolder;
//        }
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        News news = list.get(position);
//        Log.i("tt", news.getCategory());
        String title = news.getTitle();
        String time = news.getTime();
        String src = news.getSrc();
        String category = news.getCategory();
        String pic = news.getPic();
        String content = news.getContent();
        String url = news.getUrl();
        String WebUrl = news.getWeburl();
//        holder.tvNewsTitle.setText(title);
//        holder.tvNewsSrcTime.setText(src + " " + time);
//        holder.tvNewsTitle.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                Log.i("tt", news.getTitle());
//                Intent intent = new Intent(context, NewContentActivity.class);
//                Bundle bundle = new Bundle();
//                bundle.putString("title", title);
//                bundle.putString("time", time);
//                bundle.putString("src", src);
//                bundle.putString("category", category);
//                bundle.putString("pic", pic);
//                bundle.putString("content", content);
//                bundle.putString("url", url);
//                bundle.putString("WebUrl", WebUrl);
//                intent.putExtras(bundle);
//                context.startActivity(intent);
//            }
//        });
//        if (list.get(position).getCategory().equals(News.IMAGE_NEWS)) {
            holder.tvNewsTitle1.setText(news.getTitle());
            holder.tvNewsSrcTime1.setText(news.getSrc() + " " + news.getTime());
            Glide.with(context).load(pic).into(holder.imageView2);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, NewContentActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("title", title);
                    bundle.putString("time", time);
                    bundle.putString("src", src);
                    bundle.putString("category", category);
                    bundle.putString("pic", pic);
                    bundle.putString("content", content);
                    bundle.putString("url", url);
                    bundle.putString("WebUrl", WebUrl);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });
//            }
//        } else {
//            holder.tvNewsTitle.setText(news.getTitle());
//            holder.tvNewsSrcTime.setText(news.getSrc() + " " + news.getTime());
//        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
//        TextView tvNewsTitle, tvNewsSrcTime;
        TextView tvNewsTitle1, tvNewsSrcTime1;
        ImageView imageView2;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
//            this.tvNewsTitle = itemView.findViewById(R.id.tvNewsTitle);
//            this.tvNewsSrcTime = itemView.findViewById(R.id.tvNewsSrcTime);
            this.tvNewsTitle1 = itemView.findViewById(R.id.tvNewsTitle1);
            this.tvNewsSrcTime1 = itemView.findViewById(R.id.tvNewsSrcTime1);
            this.imageView2 = itemView.findViewById(R.id.imageView2);
        }
    }
}
